﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LCARSLibrary.VerbalInterface;
using LCARSLibrary;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using LCARSLibrary.Controls;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

using Settings = Cepheus.Settings<LCARS_Interface.MISettings>;


namespace LCARS_Interface
{
    //partial class FormMain
    //{
    //    VerbalInterface verbalInterface;

    //    VIGrammarGroup grConfirmation;
    //    VIGrammarGroup grConfirmation2;
    //    VIGrammarGroup grCommands;
    //    VIGrammarGroup grActivites;
    //    VIGrammarGroup grMusic;
    //    VIGrammarGroup grMusic2;
    //    VIGrammarGroup grAdminMode;

    //    Dictionary<int, VIGrammarGroup> activityProgramsGrammars = new Dictionary<int, VIGrammarGroup>();

    //    int viGrammarIDCounter = 1;


    //    void InitializeVI()
    //    {
    //        grCommands = new VIGrammarGroup() { Grammars = new List<Grammar>(new[] { CreateGrCommands()/*, CreateGrMultimediaCtrl()*/ }), ID = 0 };

    //        grActivites = new VIGrammarGroup() { Grammars = new List<Grammar>(new[] { CreateGrActivites() }), ID = viGrammarIDCounter++ };

    //        grConfirmation = new VIGrammarGroup() { Grammars = new List<Grammar>(new[] { CreateGrConfirmation() }), ID = viGrammarIDCounter++ };
    //        grConfirmation.CommandRejected += new EventHandler<RejectedCommandArgs>(grConfirmation_CommandRejected);

    //        grConfirmation2 = new VIGrammarGroup() { Grammars = new List<Grammar>(new[] { CreateGrConfirmation2() }), ID = viGrammarIDCounter++ };
    //        grConfirmation2.CommandRejected += new EventHandler<RejectedCommandArgs>(grConfirmation_CommandRejected);

    //        grMusic = new VIGrammarGroup() { Grammars = new List<Grammar>(new[] { CreateGrMusic() }), ID = viGrammarIDCounter++ };

    //        grMusic2 = new VIGrammarGroup() { Grammars = new List<Grammar>(new[] { CreateGrMusic2() }), ID = viGrammarIDCounter++ };

    //        grAdminMode = new VIGrammarGroup() { Grammars = new List<Grammar>(new[] { CreateGrAdminMode() }), ID = viGrammarIDCounter++ };
    //        grAdminMode.CommandRejected += new EventHandler<RejectedCommandArgs>(grAdminMode_CommandRejected);

    //        List<VIGrammarGroup> viGrammarGroups = new List<VIGrammarGroup>();
    //        viGrammarGroups.Add(grCommands);
    //        viGrammarGroups.Add(grActivites);
    //        viGrammarGroups.Add(grConfirmation);
    //        viGrammarGroups.Add(grConfirmation2);
    //        viGrammarGroups.Add(grMusic);
    //        viGrammarGroups.Add(grMusic2);
    //        viGrammarGroups.Add(grAdminMode);

    //        foreach (var item in Settings.Default.Data.Activites)
    //        {
    //            VIGrammarGroup vg = new VIGrammarGroup() { Grammars = new List<Grammar>(new[] { CreateGrActivityPrograms(item) }), ID = viGrammarIDCounter++ };
    //            viGrammarGroups.Add(vg);
    //            activityProgramsGrammars.Add(item.ID, vg);
    //        }


    //        verbalInterface = new VerbalInterface(VIMode.Default);
    //        verbalInterface.BaseCommandRecognized += new EventHandler<RecognizedCommandArgs>(verbalInterface_CommandRecognized);
    //        verbalInterface.BaseCommandRecognizing += new EventHandler<RecognizingCommandArgs>(verbalInterface_CommandRecognizing);
    //        verbalInterface.DeactivationCompleted += new EventHandler<DeactivationArgs>(verbalInterface_DeactivationCompleted);
    //        //verbalInterface.CommandRejected += new EventHandler<RejectedCommandArgs>(verbalInterface_CommandRejected);
    //        verbalInterface.InitializationCompleted += new EventHandler(verbalInterface_InitializationCompleted);

    //        verbalInterface.Initialize(Settings.Default.Data.BaseCommand, viGrammarGroups, 0);

    //        area28.SoundEffect = SoundEffect.Disabled;
    //        area28.Color = Colors.Default1;
    //    }

    //    void grAdminMode_CommandRejected(object sender, RejectedCommandArgs e)
    //    {
    //        foreach (var item in LCARSBase.GetGroupControls(ControlGroup.Group15))
    //            item.Color = Colors.GUISecondary;
    //    }


    //    void grConfirmation_CommandRejected(object sender, RejectedCommandArgs e)
    //    {
    //        if (e.ByTimeout)
    //            LCARSBase.PlaySoundEffectSync(SoundEffect.No1);
    //    }


    //    Grammar CreateGrConfirmation()
    //    {
    //        GrammarBuilder grammarBuilder = new GrammarBuilder();
    //        Choices choices = new Choices();

    //        choices.Add(new SemanticResultValue("Nein", 0));
    //        choices.Add(new SemanticResultValue("Negativ", 0));
    //        choices.Add(new SemanticResultValue("Ja", 1));
    //        choices.Add(new SemanticResultValue("Positiv", 1));
    //        choices.Add(new SemanticResultValue("Ich bestätige", 1));

    //        grammarBuilder.Append(choices);

    //        Grammar grammar = new Grammar(grammarBuilder);
    //        grammar.Name = "grConfirmation";

    //        return grammar;
    //    }

    //    Grammar CreateGrConfirmation2()
    //    {
    //        GrammarBuilder grammarBuilder = new GrammarBuilder();
    //        GrammarBuilder subGrammarBuilder = new GrammarBuilder();
    //        Choices choices = new Choices();

    //        //--
    //        subGrammarBuilder = new GrammarBuilder();
    //        subGrammarBuilder.AppendWildcard();
    //        //--
    //        choices.Add(subGrammarBuilder);
    //        choices.Add(new SemanticResultValue("Epsilon Pi 1 5", 1));

    //        grammarBuilder.Append(choices);
    //        Grammar grammar = new Grammar(grammarBuilder);
    //        return grammar;
    //    }

    //    Grammar CreateGrCommands()
    //    {
    //        GrammarBuilder grammarBuilder = new GrammarBuilder();
    //        GrammarBuilder subGrammarBuilder = new GrammarBuilder();
    //        Choices choices = new Choices();
    //        Choices subChoices = new Choices();

    //        //--
    //        subGrammarBuilder = new GrammarBuilder();
    //        subGrammarBuilder.Append(new SemanticResultValue("Aktivitäten", 0));

    //        subChoices = new Choices();
    //        subChoices.Add("öffnen");
    //        subChoices.Add("anzeigen");
    //        subGrammarBuilder.Append(subChoices, 0, 1);
    //        //--
    //        choices.Add(subGrammarBuilder);

    //        //--
    //        subGrammarBuilder = new GrammarBuilder();
    //        subGrammarBuilder.Append(new SemanticResultValue("Musik", 1));

    //        subChoices = new Choices();
    //        subChoices.Add("öffnen");
    //        subChoices.Add("anzeigen");
    //        subGrammarBuilder.Append(subChoices, 0, 1);
    //        //--
    //        choices.Add(subGrammarBuilder);

    //        choices.Add(new SemanticResultValue("Grafisches Interface beenden", 2));
    //        choices.Add(new SemanticResultValue("Grafisches Interface anzeigen", 3));



    //        //--
    //        subGrammarBuilder = new GrammarBuilder();
    //        subGrammarBuilder.Append(new SemanticResultValue("Wie spät", 4));

    //        subChoices = new Choices();
    //        subChoices.Add("ist es");
    //        subChoices.Add("isses");
    //        subGrammarBuilder.Append(subChoices, 0, 1);
    //        //--
    //        choices.Add(subGrammarBuilder);


    //        choices.Add(new SemanticResultValue("Administratormodus aktivieren", 5));


    //        grammarBuilder.Append(choices);

    //        Grammar grammar = new Grammar(grammarBuilder);
    //        grammar.Name = "grCommands";

    //        return grammar;
    //    }

    //    Grammar CreateGrActivites()
    //    {
    //        GrammarBuilder grammarBuilder = new GrammarBuilder();
    //        Choices choices;

    //        choices = new Choices();

    //        foreach (var item in Settings.Default.Data.Activites)
    //        {
    //            choices.Add(new SemanticResultValue(item.Name, item.ID));
    //        }

    //        grammarBuilder.Append(choices);

    //        choices = new Choices();
    //        choices.Add("öffnen");
    //        choices.Add("anzeigen");
    //        grammarBuilder.Append(choices, 0, 1);

    //        Grammar grammar = new Grammar(grammarBuilder);
    //        grammar.Name = "grActivites";

    //        return grammar;
    //    }

    //    Grammar CreateGrActivityPrograms(Activity activity)
    //    {
    //        GrammarBuilder grammarBuilder = new GrammarBuilder();
    //        Choices choices;

    //        choices = new Choices();

    //        foreach (var item in activity.Programs)
    //        {
    //            choices.Add(new SemanticResultValue(item.Name, item.ID));
    //        }

    //        grammarBuilder.Append(choices);

    //        choices = new Choices();
    //        choices.Add("starten");
    //        grammarBuilder.Append(choices, 0, 1);

    //        Grammar grammar = new Grammar(grammarBuilder);

    //        return grammar;
    //    }

    //    Grammar CreateGrMusic()
    //    {
    //        GrammarBuilder grammarBuilder = new GrammarBuilder();
    //        Choices choices = new Choices();

    //        //grammarBuilder.AppendWildcard();

    //        foreach (var item in musicAlbums)
    //        {
    //            GrammarBuilder subGrammarBuilder = new GrammarBuilder();

    //            foreach (var subItem in Path.GetFileName(item).Split(new[] { ' ' }))
    //            {
    //                subGrammarBuilder.Append(subItem, 0, 1);
    //            }

    //            choices.Add(new SemanticResultValue(subGrammarBuilder, item));

    //            //choices.Add(new SemanticResultValue(Path.GetFileName(item), item));

    //        }
    //        grammarBuilder.Append(choices);

    //        choices = new Choices();
    //        choices.Add("starten");
    //        choices.Add("spielen");
    //        choices.Add("abspielen");
    //        grammarBuilder.Append(choices, 0, 1);

    //        Grammar grammar = new Grammar(grammarBuilder);

    //        return grammar;
    //    }

    //    Grammar CreateGrMusic2()
    //    {
    //        GrammarBuilder grammarBuilder = new GrammarBuilder();
    //        Choices choices = new Choices();

    //        grammarBuilder.Append("Nein", 0, 10);

    //        choices.Add("Was anderes");
    //        choices.Add("Nächstes");
    //        choices.Add("Das nächste");
    //        choices.Add("Etwas anderes");
    //        grammarBuilder.Append(choices);

    //        Grammar grammar = new Grammar(grammarBuilder);

    //        return grammar;
    //    }

    //    Grammar CreateGrMultimediaCtrl()
    //    {
    //        GrammarBuilder grammarBuilder = new GrammarBuilder();
    //        GrammarBuilder subGrammarBuilder = new GrammarBuilder();
    //        Choices choices = new Choices();
    //        Choices subChoices = new Choices();

    //        //--
    //        subGrammarBuilder.Append("Wiedergabe");

    //        subChoices.Add(new SemanticResultValue("pausieren", 0));
    //        subChoices.Add(new SemanticResultValue("fortsetzen", 1));
    //        subChoices.Add(new SemanticResultValue("starten", 1));
    //        subChoices.Add(new SemanticResultValue("stoppen", 2));
    //        subChoices.Add(new SemanticResultValue("beenden", 2));
    //        subGrammarBuilder.Append(subChoices);
    //        //--
    //        choices.Add(subGrammarBuilder);

    //        //--
    //        subGrammarBuilder = new GrammarBuilder();
    //        subChoices = new Choices();
    //        subChoices.Add(new SemanticResultValue("Nächstes", 3));
    //        subChoices.Add(new SemanticResultValue("Nächster Titel", 3));
    //        subGrammarBuilder.Append(subChoices);
    //        //--
    //        choices.Add(subGrammarBuilder);

    //        //--
    //        subGrammarBuilder = new GrammarBuilder();
    //        subChoices = new Choices();
    //        subChoices.Add(new SemanticResultValue("Vorheriges", 4));
    //        subChoices.Add(new SemanticResultValue("Vorheriger Titel", 4));
    //        subGrammarBuilder.Append(subChoices);
    //        //--
    //        choices.Add(subGrammarBuilder);

    //        //--
    //        subGrammarBuilder = new GrammarBuilder();
    //        subGrammarBuilder.Append("Lautstärke");

    //        subChoices = new Choices();
    //        subChoices.Add(new SemanticResultValue("erhöhen", 5));
    //        subChoices.Add(new SemanticResultValue("verringern", 6));
    //        subGrammarBuilder.Append(subChoices);
    //        //--
    //        choices.Add(subGrammarBuilder);

    //        //--
    //        subGrammarBuilder = new GrammarBuilder();
    //        subGrammarBuilder.Append(new SemanticResultValue("Stummschalten", 7));
    //        //--
    //        choices.Add(subGrammarBuilder);


    //        grammarBuilder.Append(choices);


    //        Grammar grammar = new Grammar(grammarBuilder);
    //        grammar.Name = "grMultimediaCtrl";

    //        return grammar;
    //    }

    //    Grammar CreateGrAdminMode()
    //    {
    //        GrammarBuilder grammarBuilder = new GrammarBuilder();
    //        GrammarBuilder subGrammarBuilder = new GrammarBuilder();
    //        Choices choices = new Choices();
    //        Choices subChoices = new Choices();

    //        choices.Add(new SemanticResultValue("Administratormodus deaktivieren", 0));
    //        choices.Add(new SemanticResultValue("Aktivität hinzufügen", 1));

    //        grammarBuilder.Append(choices);

    //        Grammar grammar = new Grammar(grammarBuilder);
    //        return grammar;
    //    }





    //    void verbalInterface_DeactivationCompleted(object sender, DeactivationArgs e)
    //    {
    //        if (e.UserState != null && (int)e.UserState == 1)
    //        {
    //            Close();
    //        }
    //        else
    //        {
    //            area28.SoundEffect = SoundEffect.Message6;
    //            area28.Flash(true, Colors.GUIAuxiliary3);
    //        }
    //    }

    //    void verbalInterface_InitializationCompleted(object sender, EventArgs e)
    //    {
    //        area28.SoundEffect = SoundEffect.Beep4;
    //        area28.Color = Colors.GUISecondary;
    //        verbalInterface.Activate();
    //    }



    //    void verbalInterface_CommandRejected(object sender, RejectedCommandArgs e)
    //    {
    //        if (e.ByTimeout && e.Tag != null && (int)e.Tag == 42)
    //            LCARSBase.PlaySoundEffectSync(SoundEffect.No1);
    //        else if (e.VIGrammarGroup == grAdminMode)
    //        {
    //            foreach (var item in LCARSBase.GetGroupControls(ControlGroup.Group15))
    //                item.Color = Colors.GUISecondary;
    //        }

    //    }

    //    void verbalInterface_CommandRecognizing(object sender, RecognizingCommandArgs e)
    //    {
    //        if (e.VIResult.VIGrammarGroup == grConfirmation
    //            || e.VIResult.VIGrammarGroup == grConfirmation2
    //            || (e.VIResult.VIGrammarGroup == grCommands
    //                && e.VIResult.Grammar.Name != "grMultimediaCtrl"
    //                && ((int)e.VIResult.Semantics.Value == 2
    //                    || (int)e.VIResult.Semantics.Value == 5)
    //                    )
    //            )
    //        {
    //            e.NoSoundEffect = true;
    //        }
    //        else if (e.VIResult.VIGrammarGroup == grMusic2)
    //        {
    //            verbalInterface.StopRejectTimeout();

    //            excludedPaths.Add(currentPath);

    //            var d = from x in lastPaths
    //                    where !excludedPaths.Contains(x)
    //                    select x;

    //            lastPaths = d.ToList();

    //            if (lastPaths.Count == 0)
    //            {
    //                e.Cancel = true;
    //                LCARSBase.PlaySoundEffectSync(SoundEffect.No2);
    //            }
    //        }
    //    }

    //    void verbalInterface_CommandRecognized(object sender, RecognizedCommandArgs e)
    //    {
    //        if (e.VIResult.VIGrammarGroup == null)
    //        {
    //            e.NextVIGrammarGroup = grCommands;
    //        }
    //        #region grCommands
    //        else if (e.VIResult.VIGrammarGroup == grCommands)
    //        {
    //            if (e.VIResult.Grammar.Name == "grMultimediaCtrl")
    //            {
    //                switch ((int)e.VIResult.Semantics.Value)
    //                {
    //                    case 0:
    //                        vlcInterface.Pause();
    //                        break;
    //                    case 1:
    //                        vlcInterface.Play();
    //                        break;
    //                    case 2:
    //                        vlcInterface.Stop();
    //                        break;
    //                    case 3:
    //                        vlcInterface.NextTrack();
    //                        break;
    //                    case 4:
    //                        vlcInterface.PreviousTrack();
    //                        break;
    //                    case 5:
    //                        vlcInterface.VolumeUp();
    //                        break;
    //                    case 6:
    //                        vlcInterface.VolumeDown();
    //                        break;
    //                    case 7:
    //                        vlcInterface.VolumeMute();
    //                        break;
    //                }
    //            }
    //            else if ((int)e.VIResult.Semantics.Value == 0)
    //            {
    //                e.NextVIGrammarGroup = grActivites;
    //                pagingActivities.Show();
    //            }
    //            else if ((int)e.VIResult.Semantics.Value == 1)
    //            {
    //                e.NextVIGrammarGroup = grMusic;
    //                pagingMusic.Show();
    //            }
    //            else if ((int)e.VIResult.Semantics.Value == 2)
    //            {
    //                LCARSBase.PlaySoundEffectSync(SoundEffect.Message6);

    //                using (SpeechSynthesizer syn = new SpeechSynthesizer())
    //                    syn.Speak("Bitte bestätigen...");

    //                e.NextVIGrammarGroup = grConfirmation;
    //                verbalInterface.StartRejectTimeout(10000, 42);
    //            }
    //            else if ((int)e.VIResult.Semantics.Value == 3)
    //            {
    //                if (!this.Visible)
    //                    areaMinimize.PlaySoundEffect();

    //                this.Show();
    //                SetForegroundWindow(this.Handle);
    //                this.Focus();
    //            }
    //            else if ((int)e.VIResult.Semantics.Value == 4)
    //            {
    //                using (SpeechSynthesizer syn = new SpeechSynthesizer())
    //                    syn.Speak(string.Format("Es ist {0} Uhr {1}.", DateTime.Now.Hour, DateTime.Now.Minute));
    //            }
    //            else if ((int)e.VIResult.Semantics.Value == 5)
    //            {
    //                LCARSBase.PlaySoundEffectSync(SoundEffect.Message6);

    //                using (SpeechSynthesizer syn = new SpeechSynthesizer())
    //                    syn.Speak("Bitte bestätigen...");

    //                e.NextVIGrammarGroup = grConfirmation2;
    //                verbalInterface.StartRejectTimeout(10000, 42);
    //            }
    //        }
    //        #endregion
    //        #region grConfirmation
    //        else if (e.VIResult.VIGrammarGroup == grConfirmation)
    //        {
    //            verbalInterface.StopRejectTimeout();

    //            if ((int)e.VIResult.Semantics.Value == 1)
    //            {
    //                LCARSBase.PlaySoundEffectSync(SoundEffect.Yes1);
    //                verbalInterface.Deactivate(1);
    //            }
    //            else
    //            {
    //                LCARSBase.PlaySoundEffectSync(SoundEffect.No1);
    //            }
    //        }
    //        #endregion
    //        #region grConfirmation2
    //        else if (e.VIResult.VIGrammarGroup == grConfirmation2)
    //        {
    //            verbalInterface.StopRejectTimeout();

    //            if (e.VIResult.Semantics.Value != null && (int)e.VIResult.Semantics.Value == 1)
    //            {
    //                LCARSBase.PlaySoundEffectSync(SoundEffect.Yes1);
    //                foreach (var item in LCARSBase.GetGroupControls(ControlGroup.Group15))
    //                    item.Color = Colors.BlueAlertGUIPrimary;

    //                e.NextVIGrammarGroup = grAdminMode;
    //            }
    //            else
    //            {
    //                LCARSBase.PlaySoundEffectSync(SoundEffect.No1);
    //            }
    //        }
    //        #endregion
    //        #region grActivites
    //        else if (e.VIResult.VIGrammarGroup == grActivites)
    //        {
    //            Activity activity = pagingActivities.GetEntryByID((int)e.VIResult.Semantics.Value);
    //            pagingActivityPrograms.Data = activity.Programs;
    //            pagingActivityPrograms.Show();

    //            e.NextVIGrammarGroup = activityProgramsGrammars[activity.ID];
    //        }
    //        #endregion
    //        #region grMusic
    //        else if (e.VIResult.VIGrammarGroup == grMusic)
    //        {
    //            excludedPaths.Clear();

    //            var d = from x in musicAlbums
    //                    where Path.GetFileName(x).Contains(e.VIResult.Text)
    //                    select x;

    //            lastPaths = d.ToList();

    //            Random randomizer = new Random(DateTime.Now.Millisecond);
    //            currentPath = lastPaths[randomizer.Next(lastPaths.Count - 1)];

    //            Process process = new Process();
    //            process.StartInfo.FileName = Settings.Default.Data.VLCPath;
    //            process.StartInfo.Arguments = string.Format("--started-from-file --no-qt-notification --qt-start-minimized \"{0}\"", currentPath/*e.SemanticValue.Value.ToString()*/);
    //            process.Start();

    //            e.NextVIGrammarGroup = grMusic2;
    //            verbalInterface.StartRejectTimeout(20000);
    //        }
    //        #endregion
    //        #region grMusic2
    //        else if (e.VIResult.VIGrammarGroup == grMusic2)
    //        {
    //            if (lastPaths.Count != 0)
    //            {
    //                Random randomizer = new Random(DateTime.Now.Millisecond);
    //                currentPath = lastPaths[randomizer.Next(lastPaths.Count - 1)];

    //                Process process = new Process();
    //                process.StartInfo.FileName = Settings.Default.Data.VLCPath;
    //                process.StartInfo.Arguments = string.Format("--started-from-file --no-qt-notification --qt-start-minimized \"{0}\"", currentPath/*e.SemanticValue.Value.ToString()*/);
    //                process.Start();

    //                e.NextVIGrammarGroup = grMusic2;
    //                verbalInterface.StartRejectTimeout(20000);
    //            }
    //        }
    //        #endregion
    //        #region activityProgramsGrammars
    //        else if (activityProgramsGrammars.Values.Contains(e.VIResult.VIGrammarGroup))
    //        {
    //            Activity.Program activityProgram = pagingActivityPrograms.GetEntryByID((int)e.VIResult.Semantics.Value);

    //            Process process = new Process();
    //            process.StartInfo.WorkingDirectory = Path.GetDirectoryName(activityProgram.Path);
    //            process.StartInfo.FileName = activityProgram.Path;
    //            process.Start();
    //        }
    //        #endregion
    //        #region grAdminMode
    //        else if (e.VIResult.VIGrammarGroup == grAdminMode)
    //        {
    //            if ((int)e.VIResult.Semantics.Value == 0)
    //            {
    //                foreach (var item in LCARSBase.GetGroupControls(ControlGroup.Group15))
    //                    item.Color = Colors.GUISecondary;
    //            }
    //            else if ((int)e.VIResult.Semantics.Value == 1)
    //            {
    //                FormChangeSettings f = new FormChangeSettings();
    //                f.Show();
    //                //OpenFileDialog ofd = new OpenFileDialog();
    //                //ofd.ShowDialog();

    //                e.NextVIGrammarGroup = grAdminMode;
    //            }
    //        }
    //        #endregion
    //    }


    //    List<string> lastPaths = new List<string>();
    //    string currentPath;
    //    List<string> excludedPaths = new List<string>();
    //}

    partial class FormMain
    {
        VerbalInterface verbalInterface;

        VIGrammarGroup grConfirmation;
        VIGrammarGroup grConfirmation2;
        VIGrammarGroup grCommands;
        VIGrammarGroup grActivites;
        VIGrammarGroup grMusic;
        VIGrammarGroup grMusic2;
        VIGrammarGroup grAdminMode;

        Dictionary<int, VIGrammarGroup> activityProgramsGrammars = new Dictionary<int, VIGrammarGroup>();

        int viGrammarIDCounter = 1;


        List<string> lastPaths = new List<string>();
        string currentPath;
        List<string> excludedPaths = new List<string>();

        void InitializeVI()
        {
            grCommands = new VIGrammarGroup() { Grammars = new List<Grammar>(new[] { CreateGrCommands(), CreateGrMultimediaCtrl() }), ID = 0 };
            grCommands.CommandRecognizing += new EventHandler<RecognizingCommandArgs>(grCommands_CommandRecognizing);
            grCommands.CommandRecognized += new EventHandler<RecognizedCommandArgs>(grCommands_CommandRecognized);

            grActivites = new VIGrammarGroup() { Grammars = new List<Grammar>(new[] { CreateGrActivites() }), ID = viGrammarIDCounter++ };
            grActivites.CommandRecognized += new EventHandler<RecognizedCommandArgs>(grActivites_CommandRecognized);

            grConfirmation = new VIGrammarGroup() { Grammars = new List<Grammar>(new[] { CreateGrConfirmation() }), ID = viGrammarIDCounter++ };
            grConfirmation.CommandRejected += new EventHandler<RejectedCommandArgs>(grConfirmation_CommandRejected);
            grConfirmation.CommandRecognizing += new EventHandler<RecognizingCommandArgs>(grConfirmation_CommandRecognizing);
            grConfirmation.CommandRecognized += new EventHandler<RecognizedCommandArgs>(grConfirmation_CommandRecognized);

            grConfirmation2 = new VIGrammarGroup() { Grammars = new List<Grammar>(new[] { CreateGrConfirmation2() }), ID = viGrammarIDCounter++ };
            grConfirmation2.CommandRejected += new EventHandler<RejectedCommandArgs>(grConfirmation_CommandRejected);
            grConfirmation2.CommandRecognizing += new EventHandler<RecognizingCommandArgs>(grConfirmation_CommandRecognizing);
            grConfirmation2.CommandRecognized += new EventHandler<RecognizedCommandArgs>(grConfirmation2_CommandRecognized);

            grMusic = new VIGrammarGroup() { Grammars = new List<Grammar>(new[] { CreateGrMusic() }), ID = viGrammarIDCounter++ };
            grMusic.CommandRecognized += new EventHandler<RecognizedCommandArgs>(grMusic_CommandRecognized);

            grMusic2 = new VIGrammarGroup() { Grammars = new List<Grammar>(new[] { CreateGrMusic2() }), ID = viGrammarIDCounter++ };
            grMusic2.CommandRecognizing += new EventHandler<RecognizingCommandArgs>(grMusic2_CommandRecognizing);
            grMusic2.CommandRecognized += new EventHandler<RecognizedCommandArgs>(grMusic2_CommandRecognized);

            grAdminMode = new VIGrammarGroup() { Grammars = new List<Grammar>(new[] { CreateGrAdminMode() }), ID = viGrammarIDCounter++ };
            grAdminMode.CommandRejected += new EventHandler<RejectedCommandArgs>(grAdminMode_CommandRejected);
            grAdminMode.CommandRecognized += new EventHandler<RecognizedCommandArgs>(grAdminMode_CommandRecognized);


            List<VIGrammarGroup> viGrammarGroups = new List<VIGrammarGroup>();
            viGrammarGroups.AddRange(new[] 
            { 
                grCommands,
                grActivites,
                grConfirmation, 
                grConfirmation2, 
                grMusic, 
                grMusic2, 
                grAdminMode 
            });

            foreach (var item in Settings.Default.Data.Activites)
            {
                VIGrammarGroup vg = new VIGrammarGroup() { Grammars = new List<Grammar>(new[] { CreateGrActivityPrograms(item) }), ID = viGrammarIDCounter++ };
                vg.CommandRecognized += new EventHandler<RecognizedCommandArgs>(activityProgramsGrammar_CommandRecognized);
                viGrammarGroups.Add(vg);
                activityProgramsGrammars.Add(item.ID, vg);
            }


            verbalInterface = new VerbalInterface(VIMode.Default);
            verbalInterface.BaseCommandRecognized += new EventHandler<RecognizedCommandArgs>(verbalInterface_BaseCommandRecognized);
            verbalInterface.DeactivationCompleted += new EventHandler<DeactivationArgs>(verbalInterface_DeactivationCompleted);
            verbalInterface.InitializationCompleted += new EventHandler(verbalInterface_InitializationCompleted);

            verbalInterface.Initialize(Settings.Default.Data.BaseCommand, viGrammarGroups, 0);

            area28.SoundEffect = SoundEffect.Disabled;
            area28.Color = Colors.Default1;
        }      

        #region Grammar creation

        Grammar CreateGrConfirmation()
        {
            GrammarBuilder grammarBuilder = new GrammarBuilder();
            Choices choices = new Choices();

            choices.Add(new SemanticResultValue("Nein", 0));
            choices.Add(new SemanticResultValue("Negativ", 0));
            choices.Add(new SemanticResultValue("Ja", 1));
            choices.Add(new SemanticResultValue("Positiv", 1));
            choices.Add(new SemanticResultValue("Ich bestätige", 1));

            grammarBuilder.Append(choices);

            Grammar grammar = new Grammar(grammarBuilder);
            grammar.Name = "grConfirmation";

            return grammar;
        }

        Grammar CreateGrConfirmation2()
        {
            GrammarBuilder grammarBuilder = new GrammarBuilder();
            GrammarBuilder subGrammarBuilder = new GrammarBuilder();
            Choices choices = new Choices();

            //--
            subGrammarBuilder = new GrammarBuilder();
            subGrammarBuilder.AppendWildcard();
            //--
            choices.Add(subGrammarBuilder);
            choices.Add(new SemanticResultValue("Epsilon Pi 1 5", 1));

            grammarBuilder.Append(choices);
            Grammar grammar = new Grammar(grammarBuilder);
            return grammar;
        }

        Grammar CreateGrCommands()
        {
            GrammarBuilder grammarBuilder = new GrammarBuilder();
            GrammarBuilder subGrammarBuilder = new GrammarBuilder();
            Choices choices = new Choices();
            Choices subChoices = new Choices();

            //--
            subGrammarBuilder = new GrammarBuilder();
            subGrammarBuilder.Append(new SemanticResultValue("Aktivitäten", 0));

            subChoices = new Choices();
            subChoices.Add("öffnen");
            subChoices.Add("anzeigen");
            subGrammarBuilder.Append(subChoices, 0, 1);
            //--
            choices.Add(subGrammarBuilder);

            //--
            subGrammarBuilder = new GrammarBuilder();
            subGrammarBuilder.Append(new SemanticResultValue("Musik", 1));

            subChoices = new Choices();
            subChoices.Add("öffnen");
            subChoices.Add("anzeigen");
            subGrammarBuilder.Append(subChoices, 0, 1);
            //--
            choices.Add(subGrammarBuilder);

            choices.Add(new SemanticResultValue("Grafisches Interface beenden", 2));
            choices.Add(new SemanticResultValue("Grafisches Interface anzeigen", 3));



            //--
            subGrammarBuilder = new GrammarBuilder();
            subGrammarBuilder.Append(new SemanticResultValue("Wie spät", 4));

            subChoices = new Choices();
            subChoices.Add("ist es");
            subChoices.Add("isses");
            subGrammarBuilder.Append(subChoices, 0, 1);
            //--
            choices.Add(subGrammarBuilder);


            choices.Add(new SemanticResultValue("Administratormodus aktivieren", 5));


            //--
            subGrammarBuilder = new GrammarBuilder();
            subChoices = new Choices();
            subChoices.Add(new SemanticResultValue("Du nich", 6));
            subChoices.Add(new SemanticResultValue("Nicht du", 6));
            subGrammarBuilder.Append(subChoices);
            //--
            choices.Add(subGrammarBuilder);


            grammarBuilder.Append(choices);

            Grammar grammar = new Grammar(grammarBuilder);
            grammar.Name = "grCommands";

            return grammar;
        }

        Grammar CreateGrActivites()
        {
            GrammarBuilder grammarBuilder = new GrammarBuilder();
            Choices choices;

            choices = new Choices();

            foreach (var item in Settings.Default.Data.Activites)
            {
                choices.Add(new SemanticResultValue(item.Name, item.ID));
            }

            grammarBuilder.Append(choices);

            choices = new Choices();
            choices.Add("öffnen");
            choices.Add("anzeigen");
            grammarBuilder.Append(choices, 0, 1);

            Grammar grammar = new Grammar(grammarBuilder);
            grammar.Name = "grActivites";

            return grammar;
        }

        Grammar CreateGrActivityPrograms(Activity activity)
        {
            GrammarBuilder grammarBuilder = new GrammarBuilder();
            Choices choices;

            choices = new Choices();

            foreach (var item in activity.Programs)
            {
                choices.Add(new SemanticResultValue(item.Name, item.ID));
            }

            grammarBuilder.Append(choices);

            choices = new Choices();
            choices.Add("starten");
            grammarBuilder.Append(choices, 0, 1);

            Grammar grammar = new Grammar(grammarBuilder);

            return grammar;
        }

        Grammar CreateGrMusic()
        {
            GrammarBuilder grammarBuilder = new GrammarBuilder();
            Choices choices = new Choices();

            //grammarBuilder.AppendWildcard();

            foreach (var item in musicAlbums)
            {
                GrammarBuilder subGrammarBuilder = new GrammarBuilder();

                foreach (var subItem in Path.GetFileName(item).Split(new[] { ' ' }))
                {
                    subGrammarBuilder.Append(subItem, 0, 1);
                }

                choices.Add(new SemanticResultValue(subGrammarBuilder, item));

                //choices.Add(new SemanticResultValue(Path.GetFileName(item), item));

            }
            grammarBuilder.Append(choices);

            choices = new Choices();
            choices.Add("starten");
            choices.Add("spielen");
            choices.Add("abspielen");
            grammarBuilder.Append(choices, 0, 1);

            Grammar grammar = new Grammar(grammarBuilder);

            return grammar;
        }

        Grammar CreateGrMusic2()
        {
            GrammarBuilder grammarBuilder = new GrammarBuilder();
            Choices choices = new Choices();

            grammarBuilder.Append("Nein", 0, 10);

            choices.Add("Was anderes");
            choices.Add("Nächstes");
            choices.Add("Das nächste");
            choices.Add("Etwas anderes");
            grammarBuilder.Append(choices);

            Grammar grammar = new Grammar(grammarBuilder);

            return grammar;
        }

        Grammar CreateGrMultimediaCtrl()
        {
            GrammarBuilder grammarBuilder = new GrammarBuilder();
            GrammarBuilder subGrammarBuilder = new GrammarBuilder();
            Choices choices = new Choices();
            Choices subChoices = new Choices();

            //--
            subGrammarBuilder.Append("Wiedergabe");

            subChoices.Add(new SemanticResultValue("pausieren", 0));
            subChoices.Add(new SemanticResultValue("fortsetzen", 1));
            subChoices.Add(new SemanticResultValue("starten", 1));
            subChoices.Add(new SemanticResultValue("stoppen", 2));
            subChoices.Add(new SemanticResultValue("beenden", 2));
            subGrammarBuilder.Append(subChoices);
            //--
            choices.Add(subGrammarBuilder);

            //--
            subGrammarBuilder = new GrammarBuilder();
            subChoices = new Choices();
            subChoices.Add(new SemanticResultValue("Nächstes", 3));
            subChoices.Add(new SemanticResultValue("Nächster Titel", 3));
            subGrammarBuilder.Append(subChoices);
            //--
            choices.Add(subGrammarBuilder);

            //--
            subGrammarBuilder = new GrammarBuilder();
            subChoices = new Choices();
            subChoices.Add(new SemanticResultValue("Vorheriges", 4));
            subChoices.Add(new SemanticResultValue("Vorheriger Titel", 4));
            subGrammarBuilder.Append(subChoices);
            //--
            choices.Add(subGrammarBuilder);

            //--
            subGrammarBuilder = new GrammarBuilder();
            subGrammarBuilder.Append("Lautstärke");

            subChoices = new Choices();
            subChoices.Add(new SemanticResultValue("erhöhen", 5));
            subChoices.Add(new SemanticResultValue("verringern", 6));
            subGrammarBuilder.Append(subChoices);
            //--
            choices.Add(subGrammarBuilder);

            //--
            subGrammarBuilder = new GrammarBuilder();
            subGrammarBuilder.Append(new SemanticResultValue("Stummschalten", 7));
            //--
            choices.Add(subGrammarBuilder);


            grammarBuilder.Append(choices);


            Grammar grammar = new Grammar(grammarBuilder);
            grammar.Name = "grMultimediaCtrl";

            return grammar;
        }

        Grammar CreateGrAdminMode()
        {
            GrammarBuilder grammarBuilder = new GrammarBuilder();
            GrammarBuilder subGrammarBuilder = new GrammarBuilder();
            Choices choices = new Choices();
            Choices subChoices = new Choices();

            choices.Add(new SemanticResultValue("Administratormodus deaktivieren", 0));
            choices.Add(new SemanticResultValue("Aktivität hinzufügen", 1));

            grammarBuilder.Append(choices);

            Grammar grammar = new Grammar(grammarBuilder);
            return grammar;
        }

        #endregion

        void grConfirmation_CommandRecognizing(object sender, RecognizingCommandArgs e)
        {
            e.NoSoundEffect = true;
        }

        void grCommands_CommandRecognizing(object sender, RecognizingCommandArgs e)
        {
            if (e.VIResult.Grammar.Name != "grMultimediaCtrl"
            && ((int)e.VIResult.Semantics.Value == 2
                || (int)e.VIResult.Semantics.Value == 5)
                )
            {
                e.NoSoundEffect = true;
            }
        }

        void grMusic2_CommandRecognizing(object sender, RecognizingCommandArgs e)
        {
            verbalInterface.StopRejectTimeout();

            excludedPaths.Add(currentPath);

            var d = from x in lastPaths
                    where !excludedPaths.Contains(x)
                    select x;

            lastPaths = d.ToList();

            if (lastPaths.Count == 0)
            {
                e.Cancel = true;
                LCARSBase.PlaySoundEffectSync(SoundEffect.No2);
            }
        }


        void grMultimediaCtrl_CommandRecognized(object sender, RecognizedCommandArgs e)
        {
        }

        void grAdminMode_CommandRecognized(object sender, RecognizedCommandArgs e)
        {
            if ((int)e.VIResult.Semantics.Value == 0)
            {
                foreach (var item in LCARSBase.GetGroupControls(ControlGroup.Group15))
                    item.Color = Colors.Orange4;
            }
            else if ((int)e.VIResult.Semantics.Value == 1)
            {
                //OpenFileDialog ofd = new OpenFileDialog();
                //ofd.ShowDialog();

                e.NextVIGrammarGroup = grAdminMode;
            }
        }

        void grMusic2_CommandRecognized(object sender, RecognizedCommandArgs e)
        {
            if (lastPaths.Count != 0)
            {
                Random randomizer = new Random(DateTime.Now.Millisecond);
                currentPath = lastPaths[randomizer.Next(lastPaths.Count - 1)];

                Process process = new Process();
                process.StartInfo.FileName = Settings.Default.Data.VLCPath;
                process.StartInfo.Arguments = string.Format("--started-from-file --no-qt-notification --qt-start-minimized \"{0}\"", currentPath/*e.SemanticValue.Value.ToString()*/);
                process.Start();

                e.NextVIGrammarGroup = grMusic2;
                verbalInterface.StartRejectTimeout(20000);
            }
        }

        void grMusic_CommandRecognized(object sender, RecognizedCommandArgs e)
        {
            excludedPaths.Clear();

            var d = from x in musicAlbums
                    where Path.GetFileName(x).Contains(e.VIResult.Text)
                    select x;

            lastPaths = d.ToList();

            Random randomizer = new Random(DateTime.Now.Millisecond);
            currentPath = lastPaths[randomizer.Next(lastPaths.Count - 1)];

            Process process = new Process();
            process.StartInfo.FileName = Settings.Default.Data.VLCPath;
            process.StartInfo.Arguments = string.Format("--started-from-file --no-qt-notification --qt-start-minimized \"{0}\"", currentPath/*e.SemanticValue.Value.ToString()*/);
            process.Start();

            e.NextVIGrammarGroup = grMusic2;
            verbalInterface.StartRejectTimeout(20000);
        }

        void grConfirmation2_CommandRecognized(object sender, RecognizedCommandArgs e)
        {
            verbalInterface.StopRejectTimeout();

            if (e.VIResult.Semantics.Value != null && (int)e.VIResult.Semantics.Value == 1)
            {
                LCARSBase.PlaySoundEffectSync(SoundEffect.Yes1);
                foreach (var item in LCARSBase.GetGroupControls(ControlGroup.Group15))
                    item.Color = Colors.Blue2;

                e.NextVIGrammarGroup = grAdminMode;
            }
            else
            {
                LCARSBase.PlaySoundEffectSync(SoundEffect.No1);
            }
        }

        void grConfirmation_CommandRecognized(object sender, RecognizedCommandArgs e)
        {
            verbalInterface.StopRejectTimeout();

            if ((int)e.VIResult.Semantics.Value == 1)
            {
                LCARSBase.PlaySoundEffectSync(SoundEffect.Yes1);
                verbalInterface.Deactivate(1);
            }
            else
            {
                LCARSBase.PlaySoundEffectSync(SoundEffect.No1);
            }
        }

        void activityProgramsGrammar_CommandRecognized(object sender, RecognizedCommandArgs e)
        {
            Activity.Program activityProgram = pagingActivityPrograms.GetEntryByID((int)e.VIResult.Semantics.Value);

            Process process = new Process();
            process.StartInfo.WorkingDirectory = Path.GetDirectoryName(activityProgram.Path);
            process.StartInfo.FileName = activityProgram.Path;
            process.Start();
        }

        void grActivites_CommandRecognized(object sender, RecognizedCommandArgs e)
        {
            Activity activity = pagingActivities.GetEntryByID((int)e.VIResult.Semantics.Value);
            pagingActivityPrograms.Data = activity.Programs;
            pagingActivityPrograms.Show();

            e.NextVIGrammarGroup = activityProgramsGrammars[activity.ID];
        }

        void grCommands_CommandRecognized(object sender, RecognizedCommandArgs e)
        {
            if (e.VIResult.Grammar.Name == "grMultimediaCtrl")
            {
                switch ((int)e.VIResult.Semantics.Value)
                {
                    case 0:
                        vlcInterface.Pause();
                        break;
                    case 1:
                        vlcInterface.Play();
                        break;
                    case 2:
                        vlcInterface.Stop();
                        break;
                    case 3:
                        vlcInterface.NextTrack();
                        break;
                    case 4:
                        vlcInterface.PreviousTrack();
                        break;
                    case 5:
                        vlcInterface.VolumeUp();
                        break;
                    case 6:
                        vlcInterface.VolumeDown();
                        break;
                    case 7:
                        vlcInterface.VolumeMute();
                        break;
                }
            }
            else if ((int)e.VIResult.Semantics.Value == 0)
            {
                e.NextVIGrammarGroup = grActivites;
                pagingActivities.Show();
            }
            else if ((int)e.VIResult.Semantics.Value == 1)
            {
                e.NextVIGrammarGroup = grMusic;
                pagingMusic.Show();
            }
            else if ((int)e.VIResult.Semantics.Value == 2)
            {
                LCARSBase.PlaySoundEffectSync(SoundEffect.Message6);

                using (SpeechSynthesizer syn = new SpeechSynthesizer())
                    syn.Speak("Bitte bestätigen...");

                e.NextVIGrammarGroup = grConfirmation;
                verbalInterface.StartRejectTimeout(10000, 42);
            }
            else if ((int)e.VIResult.Semantics.Value == 3)
            {
                if (!this.Visible)
                    areaMinimize.PlaySoundEffect();

                this.Show();
                SetForegroundWindow(this.Handle);
                this.Focus();
            }
            else if ((int)e.VIResult.Semantics.Value == 4)
            {
                using (SpeechSynthesizer syn = new SpeechSynthesizer())
                    syn.Speak(string.Format("Es ist {0} Uhr {1}.", DateTime.Now.Hour, DateTime.Now.Minute));
            }
            else if ((int)e.VIResult.Semantics.Value == 5)
            {
                LCARSBase.PlaySoundEffectSync(SoundEffect.Message6);

                using (SpeechSynthesizer syn = new SpeechSynthesizer())
                    syn.Speak("Bitte bestätigen...");

                e.NextVIGrammarGroup = grConfirmation2;
                verbalInterface.StartRejectTimeout(10000, 42);
            }
            else if ((int)e.VIResult.Semantics.Value == 6)
            {
                LCARSBase.PlaySoundEffect(SoundEffect.Yes1);
                verbalInterface.Deactivate();
            }
        }
       


        void grAdminMode_CommandRejected(object sender, RejectedCommandArgs e)
        {
            foreach (var item in LCARSBase.GetGroupControls(ControlGroup.Group15))
                item.Color = Colors.Orange4;
        }

        void grConfirmation_CommandRejected(object sender, RejectedCommandArgs e)
        {
            if (e.ByTimeout)
                LCARSBase.PlaySoundEffectSync(SoundEffect.No1);
        }




        void verbalInterface_BaseCommandRecognized(object sender, RecognizedCommandArgs e)
        {
            e.NextVIGrammarGroup = grCommands;
        }


        void verbalInterface_DeactivationCompleted(object sender, DeactivationArgs e)
        {
            if (e.UserState != null && (int)e.UserState == 1)
            {
                Close();
            }
            else
            {
                area28.SoundEffect = SoundEffect.Message6;
                area28.Flash(true, Colors.Blue2);
            }
        }

        void verbalInterface_InitializationCompleted(object sender, EventArgs e)
        {
            area28.SoundEffect = SoundEffect.Beep4;
            area28.Color = Colors.Orange4;
            verbalInterface.Activate();
        }
    }
}
